/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.phoenix.repository.dao;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import net.phoenix.repository.data.NodeData;

/**
 * Jigsaw server
 * 
 * @author    <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date      2012-11-15
 * @version   1.0.0
 */
public class NodeTableUpdateDao  extends JdbcDao {

	private static final String SQL_UPDATE = "update NODE set PARENT_ID=?, NAME=?, VINDEX=? where PKID=?";

	/**
	 * 更新节点的orderno
	 * 
	 * @param data
	 * @throws SQLException
	 */
	public void update(NodeData data) throws SQLException {
		PreparedStatement command = null;
		try {
			command = connection.prepareStatement(SQL_UPDATE);
			command.setString(4, data.getID());
			command.setString(1, data.getParentID());
			command.setString(2, data.getName());
			command.setInt(3, data.getOrderNo());
			command.executeUpdate();
		} finally {
			this.close(command);
		}
	}


	private static final String SQL_UPDATE_ORDER = "update NODE set PARENT_ID=? ,NAME = ?, VINDEX=? where PKID=?";

	/**
	 * 更新节点的parentID或者name
	 * 
	 * @param data
	 * @throws SQLException
	 */
	public void update(NodeData data, String oldparentID, String oldname) throws SQLException {
		CallableStatement pron = null;
		PreparedStatement command = null;
		try {
			command = connection.prepareStatement(SQL_UPDATE_ORDER);
			command.setString(1, data.getParentID());
			command.setString(2, data.getName());
			command.setInt(3, data.getOrderNo());
			command.setString(4, data.getID());
			command.executeUpdate();
		} finally {
			this.close(command);
		}
		if (oldparentID != null) {
			try {
				pron = connection.prepareCall("{ call pro_rankorderno(?,?) }");
				pron.setString(1, oldparentID);
				pron.setString(2, data.getName());
				pron.execute();
			} finally {
				pron.close();
			}
		} else if (oldname != null) {
			try {
				pron = connection.prepareCall("{ call pro_rankorderno(?,?) }");
				pron.setString(1, data.getParentID());
				pron.setString(2, oldname);
				pron.execute();
			} finally {
				pron.close();
			}
		} else {
			try {
				pron = connection.prepareCall("{ call pro_rankorderno(?,?) }");
				pron.setString(1, data.getParentID());
				pron.setString(2, data.getName());
				pron.execute();
			} finally {
				pron.close();
			}
		}

	}
	
}
